package com.xul.common.exception;

import com.xul.common.result.ResponseEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

/**
 * @author xul
 * @create 2021-11-08 20:21
 * <p>
 * 使用断言
 */
@Slf4j
public abstract class Assert {

    /**
     * 断言对象不能为空
     * 如果对象为空 则抛出异常
     *
     * @param obj
     * @param responseEnum
     */
    public static void notNull(Object obj, ResponseEnum responseEnum) {
        if (obj == null) {
            log.info("obj is null......");
            throw new BusinessException(responseEnum);
        }
    }

    /**
     * 断言对象为空
     * 如果对象不为空 则抛出异常
     *
     * @param obj
     * @param responseEnum
     */
    public static void isNull(Object obj, ResponseEnum responseEnum) {
        if (obj != null) {
            log.info("obj is not null......");
            throw new BusinessException(responseEnum);
        }
    }

    /**
     * 断言表达式为真
     * 如果不为真 则抛出异常
     *
     * @param expression
     * @param responseEnum
     */
    public static void isTrue(boolean expression, ResponseEnum responseEnum) {
        if (!expression) {
            log.info("fail......");
            throw new BusinessException(responseEnum);
        }
    }

    /**
     * 断言两个对象不相等
     * 如果相等就抛出异常
     *
     * @param o1
     * @param o2
     * @param responseEnum
     */
    public static void notEquals(Object o1, Object o2, ResponseEnum responseEnum) {
        if (o1.equals(o2)) {
            log.info("equals......");
            throw new BusinessException(responseEnum);
        }
    }

    /**
     * 断言两个对象相等
     * 如果不相等就抛出异常
     *
     * @param o1
     * @param o2
     * @param responseEnum
     */
    public static void equals(Object o1, Object o2, ResponseEnum responseEnum) {
        if (!o1.equals(o2)) {
            log.info("not equals......");
            throw new BusinessException(responseEnum);
        }
    }

    /**
     * 断言参数不为空
     * 如果为空则抛出异常
     *
     * @param s
     * @param responseEnum
     */
    public static void notEmpty(String s, ResponseEnum responseEnum) {
        if (StringUtils.isEmpty(s)) {
            log.info("is empty......");
            throw new BusinessException(responseEnum);
        }
    }
}
